package com.ali.common;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamUtil {
//Stream

    /**
     * 过滤
     */
    public static Stream<Object> filter(Stream<Object> stream, String filterValue) {
        return stream.filter((item) -> !Objects.equals(filterValue, item));
    }

    /**
     * 统计
     */
    public static long count(Stream<Object> stream) {
        return stream.count();
    }

    /**
     * 截取前多少条数据
     */
    public static Stream<Object> limit(Stream<Object> stream, long maxsize) {
        return stream.limit(maxsize);
    }

    /**
     * 跳过前多少条数据
     */
    public static Stream<Object> skip(Stream<Object> stream, long size) {
        return stream.skip(size);
    }

    /**
     * 去重
     */
    public static Stream<Object> distinct(Stream<Object> stream) {
        return stream.distinct();
    }

    /**
     * 排序
     */
    public static Stream<Object> sorted(Stream<Object> stream) {
        return stream.sorted();
    }

    /**
     * 自定义排序 - 正序
     */
    public static Stream<Object> customizeSortedAsc(Stream<Object> stream) {
        return stream.sorted(new Comparator<Object>() {
            @Override
            public int compare(Object p1, Object p2) {
                String str1 = (String) p1;
                String str2 = (String) p2;
                return str1.compareTo(str2);
            }
        });
    }

    /**
     * 自定义排序 - 倒序
     */
    public static Stream<Object> customizeSortedDesc(Stream<Object> stream) {
        return stream.sorted(new Comparator<>() {
            @Override
            public int compare(Object p1, Object p2) {
                String str1 = (String) p1;
                String str2 = (String) p2;
                return str2.compareTo(str1);
            }
        });
    }

    /**
     * 获取第一条数据
     */
    public static Object findFirst(Stream<Object> stream) {
        Optional<Object> optional = stream.findFirst();
        if (optional.isPresent()) {
            return optional.get();
        }
        return null;
    }

    /**
     * 拼接
     */
    public static Stream<Object> concat(Stream<Object> stream1, Stream<Object> stream2) {
        return Stream.concat(stream1, stream2);
    }

//Stream与Array

    /**
     * Array转化为Stream
     */
    public static Stream<Object> arrayToStream(Object[] ss) {
        return Arrays.stream(ss);
    }

    /**
     * Stream转化为Array
     */
    public static Object[] streamToArray(Stream<Object> stream) {
        return stream.toArray();
    }

//Stream与Collection

    /**
     * Collection转化为Stream
     */
    public static Stream<Object> collectionToStream(Collection<Object> collection) {
        return collection.stream();
    }

    /**
     * Stream转化为List
     */
    public static Collection<Object> streamToList(Stream<Object> stream) {
        return stream.collect(Collectors.toList());
    }

    /**
     * Stream转化为Set
     */
    public static Collection<Object> streamToSet(Stream<Object> stream) {
        return stream.collect(Collectors.toSet());
    }
}
